package xyz.scootaloo.alg.tool

import java.util.*

/**
 * @author flutterdash@qq.com
 * @since 2021/7/9 22:36
 */
object MiniTool {

    fun replaceText(
        ch1: Char, ch2: Char,
        action: (String) -> Unit = ::println) {
        val buff = StringBuilder()
        val text = readLine()!!
        for (ch in text) {
            if (ch == ch1) {
                buff.append(ch2)
            } else {
                buff.append(ch)
            }
        }
        action(buff.toString())
    }

    fun leetcodeExampleToKotlinExample2DCharArray(action: (String) -> Unit = ::println) {
        val text = readLine()!!
        val buff = StringBuilder()
        val indentation = "    "

        val from = text.indexOf('[') + 1
        val limit = text.lastIndexOf(']') - 1
        buff.append("arrayOf(\n")
        loop@ for (idx in from..limit) {
            when (val ch = text[idx]) {
                '[' -> { buff.append(indentation).append("charArrayOf(") }
                '"' -> { buff.append('\'') }
                ' ' -> { continue@loop }
                ']' -> { buff.append(')') }
                ',' -> {
                    buff.append(", ")
                    if (buff[buff.length - 3] == ')')
                        buff.append('\n')
                }
                else -> {
                    buff.append(ch)
                }
            }
        }
        buff.append("\n)")

        action(buff.toString())
    }

}

fun main() {
//    MiniTool.replaceText('\"', '\'')
    MiniTool.leetcodeExampleToKotlinExample2DCharArray()
}
